package pa165.dominatingspecies.frontend.user;

import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.StringResourceModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.springframework.dao.DataAccessException;
import org.springframework.security.access.prepost.PreAuthorize;
import pa165.dominating.species.service.UserService;
import pa165.dominating.species.tos.UserTO;

/**
 * @author Tomas Kalla
 */
@PreAuthorize("hasRole('ADMIN')")
public class DeleteUserLink extends Link<Void> {
    @SpringBean(name = "userService")
    private UserService userService;
    private final UserTO userTO;

    public DeleteUserLink(String componentId, UserTO userTO) {
        super(componentId);
        this.userTO = userTO;
    }

    @Override
    public void onClick() {
        try {
            userService.deleteUser(userTO);
        } catch (DataAccessException ex) {
            getSession().warn(new StringResourceModel("userRemoveFail", this, null, new Object[]{userTO.getId()}).getString());
            setResponsePage(UserPage.class);
        }
        getSession().info(new StringResourceModel("userRemoveSucc", this, null, new Object[]{userTO.getId()}).getString());
        setResponsePage(UserPage.class);
    }
}
